﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.EventSystems;

public class Cameracontroller : MonoBehaviour {

    public float mouseSpeed=0.05f;
    public float rotateSpeed=100;
    public float scrollwheelSpeed=200;
    public float moveSpeed=0.1f;
    public float lerpSpeed = 0.5f;
    public float lookSpeed=6;
    Vector3 mousePos;
    public static Action<TowerBase> selectTower;
    public static Action<TowerBase> exitSelectTower;
    [HideInInspector]
    public static GameObject currSelectObj;

    public LayerMask selectLayer;

    void LateUpdate() {

        MoveRotation();
        DragMove();
        ScrollMove();
        CheckTower();
        MoveToTower();
        RotateToTower();

    }
    /// <summary>
    /// 鼠标右键控制移动旋转
    /// </summary>
    private void MoveRotation()
    {
        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            ResetTower();
        }
        if (Input.GetKey(KeyCode.Mouse1))
        {
            float mouseX = Input.GetAxis("Mouse X");
            float mouseY = Input.GetAxis("Mouse Y");
            transform.Rotate(Vector3.up, Time.deltaTime * mouseX * rotateSpeed, Space.World);
            transform.Rotate(transform.right, -Time.deltaTime * mouseY * rotateSpeed, Space.World);
            Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
            Vector3 ms = new Vector3(input.x, 0, input.y) * moveSpeed;
            transform.Translate(ms, Space.Self);
        }
    }
    /// <summary>
    /// 拖拽中键移动
    /// </summary>
    private void DragMove()
    {
        if (Input.GetKeyDown(KeyCode.Mouse2))
        {
            ResetTower();
            mousePos = Input.mousePosition;
        }


        if (Input.GetKey(KeyCode.Mouse2))
        {
            Vector3 deltPos = Input.mousePosition - mousePos;
            deltPos = -deltPos;
            transform.Translate(deltPos * 0.1f, Space.Self);
            mousePos = Input.mousePosition;
        }

    }
    /// <summary>
    /// 滚动中键移动
    /// </summary>
    private void ScrollMove()
    {
        float scrollwhell = Input.GetAxis("Mouse ScrollWheel");
        transform.Translate(Vector3.forward * Time.deltaTime * scrollwhell * scrollwheelSpeed);
       
    }
    /// <summary>
    /// 选择塔
    /// </summary>
    private void CheckTower()
    {

        RaycastHit hit = default(RaycastHit);
        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000,selectLayer))
        {
            if (Input.GetKeyUp(KeyCode.Mouse0))
            {
                if (hit.collider.tag == "TowerPedestal")
                {
                    ResetTower();

                    currSelectObj = hit.collider.gameObject;
                    TowerBase tb = currSelectObj.GetComponent<TowerBase>();
                    if (EventSystem.current.IsPointerOverGameObject())
                        return;
                    if (tb != null)
                    {
                        if (selectTower != null)
                        {
                            selectTower(tb);

                        }
                    }

                    if (Vector3.Distance(currSelectObj.transform.position, transform.position) > 50)
                        toMove = true;
                    Quaternion lookAt = Quaternion.LookRotation((currSelectObj.transform.position - transform.position).normalized);
                    float angle = Quaternion.Angle(transform.rotation, lookAt);
                    if (angle > 1)
                        toRot = false;
                }
            }
            
            
        }
    }
    /// <summary>
    /// 移动到塔
    /// </summary>
    bool toMove = false;
    private void MoveToTower()
    {
        if (!toMove|| currSelectObj == null)
            return;
        if (Vector3.Distance(currSelectObj.transform.position, transform.position) < 50)
            toMove = false;
        transform.position = Vector3.Lerp(transform.position, currSelectObj.transform.position, Time.deltaTime* lerpSpeed);

    }
    /// <summary>
    /// 选择向塔
    /// </summary>
    bool toRot = false;
    private void RotateToTower()
    {
        if (!toRot||currSelectObj==null)
            return;
        Quaternion lookAt = Quaternion.LookRotation((currSelectObj.transform.position - transform.position).normalized);
        float angle = Quaternion.Angle(transform.rotation, lookAt);
        if (angle < 1)
            toRot = false;
        transform.rotation = Quaternion.Lerp(transform.rotation, lookAt, Time.deltaTime * lerpSpeed * 6);

    }
    /// <summary>
    /// 设置选择塔为null
    /// </summary>
    /// <param name="go"></param>
    public void ResetTower()
    {
        if (currSelectObj == null)
            return;
        if (currSelectObj.tag== "TowerPedestal")
        {
            TowerBase towerBase = currSelectObj.GetComponent<TowerBase>();
            if (exitSelectTower != null && towerBase != null)
                exitSelectTower(towerBase);
        }
        currSelectObj = null;
    }

    public void OnDestroy()
    {
        selectTower = null;
        exitSelectTower = null;
        currSelectObj = null;
    }

}
